package com.demo.nio;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.Executors;

/**
 * Created by chenhe17596 on 2017/6/22.
 */
public class NIODemo {

    public static void func1(){
        String filePath = "F:\\1. 基金从业资格\\考试题.txt";
        try{
            RandomAccessFile accessFile = new RandomAccessFile(filePath,"rw");
            FileChannel fileChannel = accessFile.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(48);
            int byteRead = fileChannel.read(buffer);
            while (byteRead != -1){
                System.out.println("Read " + byteRead);
                buffer.flip();
                while (buffer.hasRemaining()){
                    System.out.print((char)buffer.get());
                }
                buffer.clear();
                byteRead = fileChannel.read(buffer);
            }
            accessFile.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args){
        func1();
    }
}
